/*
 * SerialConnector.cpp
 *
 *  Created on: Aug 18, 2012
 *      Author: nam20485
 */

#include "SerialConnector.h"
#include "Logger.h"

SerialConnector::SerialConnector()
	//: TERMINATOR(-1)
{
}

SerialConnector::~SerialConnector()
{
}

bool SerialConnector::Connect(/*ConnectorReceiveHandler pfnReceiveHandler*/)
{
	Logger::log("[SerialConnector::Connect() - enter]\n\r");

	//m_pfnReceiveHandler = pfnReceiveHandler;
	Serial.begin(115200);

	Logger::log("[SerialConnector::Connect() - connected]\n\r");

	Logger::log("[SerialConnector::Connect() - exit]\n\r");

	return true;
}

bool SerialConnector::Disconnect()
{
	Serial.end();
	return true;
}

size_t SerialConnector::ReadBytes(BYTE* pBuff, size_t count)
{
	Logger::log("[SerialConnector::ReadBytes() - enter]\n\r");

	size_t read = Serial.readBytes((char*) pBuff, count);
	if (read <= 0)
	{
		Logger::log("[SerialConnector::ReadBytes() - 0 bytes read]\n\r");
	}

	Logger::log("[SerialConnector::ReadBytes() - exit]\n\r");

	return read;
}

size_t SerialConnector::WriteBytes(const BYTE* pBytes, size_t count)
{
	Logger::log("[SerialConnector::WriteBytes() - enter]\n\r");

	size_t sent = Serial.write(pBytes, count);
	if (sent <= 0)
	{
		Logger::log("[SerialConnector::WriteBytes() - 0 bytes sent]\n\r");
	}

	Logger::log("[SerialConnector::WriteBytes() - exit]\n\r");

	return sent;
}

//	LinkedList<BYTE> bytes;
//
//	// read bytes off the serial connection
//	BYTE b = TERMINATOR;
//	while (true)
//	{
//		b = Serial.read();
//		if (b != TERMINATOR)
//		{
//			// until we find a TERMINATOR
//			bytes.Add(b);
//		}
//		else
//		{
//			break;
//		}
//	}
//
//	// decode a packet from the byte stream
//	const Packet<BYTE>* pPacket = Packet<BYTE>::FromBytes(bytes);
//	if (pPacket != 0)
//	{
//		// send the message back
//		return pPacket->getMessage();
//	}

//	return false;
//}
